#include <EIKENV.H>
#include "CPainter.h"
#include "CImageHandler.h"
#include "TestUiAppUi.h"

CPainter::CPainter()
{
	iImageHandler = NULL;
}

CPainter::~CPainter()
{
	if(iImageHandler)
		delete iImageHandler;
		
	for(int i = 0; i < iImages.Count(); i++)
	{
		IMAGEDATA* imgdata = iImages[i];
		CFbsBitmap* bitmap = imgdata->iBitmap;
		delete bitmap;
		delete imgdata;
	}
	iImages.Reset();		
}

void CPainter::Draw(CBitmapContext& aGc, TRect& aClipRect)
{
	if(iImages.Count() == 0)
	return;

	IMAGEDATA* imgdata = iImages[0];
	if(imgdata->iBitmap == NULL)
		return;

	CFbsBitmap* iBitmap = imgdata->iBitmap;
	TSize bmpsize = iBitmap->SizeInPixels();
//	//create new background bitmap
	TPoint target = aClipRect.iTl;

	TRect bitmapRect=aClipRect;
	aGc.Reset();
	aGc.SetClippingRect(aClipRect);
	
	//原点
	int x = aClipRect.iTl.iX;
	int y = aClipRect.iTl.iY;
	//边距信息
	int mleft = imgdata->margin_left;
	int mright = imgdata->margin_right;
	int mtop = imgdata->margin_top;
	int mbottom = imgdata->margin_bottom;
	//中间高度和宽度
	int mw = aClipRect.Width() - mleft - mright;
	int mh = aClipRect.Height()- mtop - mbottom;
	//
	int rw = aClipRect.Width();
	int rh = aClipRect.Height();
	
	TPoint pt1 = TPoint(x,y);
	TPoint pt2 = pt1;
	TRect rc = TSize(mleft, mtop);
	aGc.BitBlt(pt1, iBitmap, rc);	//left_top
	rc = TRect(mleft, 0, bmpsize.iWidth - mleft - mright, mtop);
	pt1 = TPoint(x + mleft, y);
	pt2 = TPoint(x + mleft + mw, y + mtop);
	aGc.DrawBitmap(TRect(pt1, pt2), iBitmap, rc);
	
	rc = TRect(bmpsize.iWidth - mright, 0, bmpsize.iWidth, mtop);
	pt1 = TPoint(x + rw - mright, y);
	aGc.BitBlt(pt1, iBitmap, rc);	//right_top	
	
	rc = TRect(bmpsize.iWidth - mright, mtop, bmpsize.iWidth, bmpsize.iHeight - mbottom);
	pt1 = TPoint(x + rw - mright, y + mtop);
	pt2 = TPoint(x + rw, y + rh - mbottom);
	aGc.DrawBitmap(TRect(pt1, pt2), iBitmap, rc);

	rc = TRect(bmpsize.iWidth - mright, bmpsize.iHeight - mbottom, bmpsize.iWidth, bmpsize.iHeight);
	pt1 = TPoint(x + rw - mright, y + rh - mbottom);
	aGc.BitBlt(pt1, iBitmap, rc);	//right_bottom

	rc = TRect(mleft, bmpsize.iHeight - mbottom, bmpsize.iWidth - mright, bmpsize.iHeight);
	pt1 = TPoint(x + mleft, y + rh - mbottom);
	pt2 = TPoint(x + rw - mright, y + rh);
	aGc.DrawBitmap(TRect(pt1, pt2), iBitmap, rc);

	rc = TRect(0, bmpsize.iHeight - mbottom, mleft, bmpsize.iHeight);
	pt1 = TPoint(x, y + rh - mbottom);
	aGc.BitBlt(pt1, iBitmap, rc);	//left_bottom

	rc = TRect(0, mtop, mleft, bmpsize.iHeight - mbottom);
	pt1 = TPoint(x, y + mtop);
	pt2 = TPoint(x + mleft, y + rh - mbottom);
	aGc.DrawBitmap(TRect(pt1, pt2), iBitmap, rc);

	//center
	rc = TRect(mleft, mtop, bmpsize.iWidth - mright, bmpsize.iHeight - mbottom);
	pt1 = TPoint(x + mleft, y + mtop);
	pt2 = TPoint(x + rw - mright, y + rh - mbottom);
	aGc.DrawBitmap(TRect(pt1, pt2), iBitmap, rc);
}

void CPainter::ImageOperationComplete(TInt aError)
{
	IMAGEDATA* imgdata = new IMAGEDATA;
	imgdata->iBitmap = new(ELeave) CFbsBitmap();
	iImages.Append(imgdata);
	iImageHandler->CopyBitmap(imgdata->iBitmap);
	imgdata->margin_top = 2;
	imgdata->margin_bottom = 2;
	imgdata->margin_left = 2;
	imgdata->margin_right = 2;

	CTestUiAppUi* appui = (CTestUiAppUi*)CEikonEnv::Static()->AppUi();
	appui->TestMe2();	
}

void CPainter::LoadFileL(const TDesC& aFileName)
{
	if(iImageHandler == NULL)
	{
		iImageHandler = new (ELeave) CImageHandler(CEikonEnv::Static()->FsSession());
		iImageHandler->SetCallback(this);
	}
	
	TSize aSizeInPixel;
	iImageHandler->FastImageInfo(aFileName, aSizeInPixel);
	
	iImageHandler->LoadFileL(aFileName);
}